package com.xiaoxu.intranetweb.mybatisplusSzappdb.business.product;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.log.SysLogBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.dao.UserInfoDao;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.CProductsInfo;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.SysUser;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ICProductsInfoService;
import com.xiaoxu.intranetweb.util.SzResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PdtAdvancedBu {

    @Autowired
    private SysLogBu sysLogBu;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private ICProductsInfoService iCProductsInfoService;


    //品种的高级操作                     是否指定装载点 是否开启装载控制 是否识别装载 是否开启计划控制 是否开启排队控制 是否识别排队 进货/出货   品种来源控制
    public SzResult pdtAdvanced(String id, String loadStatus, String loadControlStatus, String identifyLoadStatus, String planControllerStatus,
                                String queueControllerStatus, String identifyQueueStatus, String inOutStatus,String pdtClassFromCtrlStatus, String token,String plantId) throws Exception {
        QueryWrapper<CProductsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);
        queryWrapper.eq("PDTINF_DELETED", 0);
        queryWrapper.eq("DATA_FROM", plantId);
        CProductsInfo cProductsInfo = iCProductsInfoService.getOne(queryWrapper);
        if (cProductsInfo == null) {
            return SzResult.error("产品不存在");
        }
        if (loadStatus != null && !loadStatus.equals("")) {
            if (loadStatus.equals("1")) {
                cProductsInfo.setPdtinfSpeLoadsite(1);
                cProductsInfo.setDownedID1(0);
                iCProductsInfoService.updateById(cProductsInfo);
                SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
                String pdtInfPdt = cProductsInfo.getPdtinfPdt();
                sysLogBu.logRecord("修改品种", user.getTrueName(), "修改了产品名称为【" + pdtInfPdt + "】的产品的是否指定装载点为【是】", plantId);
            } else {
                cProductsInfo.setPdtinfSpeLoadsite(0);
                cProductsInfo.setDownedID1(0);
                iCProductsInfoService.updateById(cProductsInfo);
                SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
                String pdtInfPdt = cProductsInfo.getPdtinfPdt();
                sysLogBu.logRecord("修改品种", user.getTrueName(), "修改了产品名称为【" + pdtInfPdt + "】的产品的是否指定装载点为【否】", plantId);
            }
        }
        if (loadControlStatus != null && !loadControlStatus.equals("")) {
            if (loadControlStatus.equals("1")) {
                cProductsInfo.setPdtinfLoadCtrl(1);
                cProductsInfo.setDownedID1(0);
                iCProductsInfoService.updateById(cProductsInfo);
                SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
                String pdtInfPdt = cProductsInfo.getPdtinfPdt();
                sysLogBu.logRecord("修改品种", user.getTrueName(), "修改了产品名称为【" + pdtInfPdt + "】的产品的是否开启装载控制为【是】",plantId);
            } else {
                cProductsInfo.setPdtinfLoadCtrl(0);
                cProductsInfo.setDownedID1(0);
                iCProductsInfoService.updateById(cProductsInfo);
                SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
                String pdtInfPdt = cProductsInfo.getPdtinfPdt();
                sysLogBu.logRecord("修改品种", user.getTrueName(), "修改了产品名称为【" + pdtInfPdt + "】的产品的是否开启装载控制为【否】",plantId);
            }
        }
        if (identifyLoadStatus != null && !identifyLoadStatus.equals("")) {
            if (identifyLoadStatus.equals("1")) {
                cProductsInfo.setPdtinfIdentyLoad(1);
                cProductsInfo.setDownedID1(0);
                iCProductsInfoService.updateById(cProductsInfo);
                SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
                String pdtInfPdt = cProductsInfo.getPdtinfPdt();
                sysLogBu.logRecord("修改品种", user.getTrueName(), "修改了产品名称为【" + pdtInfPdt + "】的产品的是否识别装载为【是】",plantId);
            } else {
                cProductsInfo.setPdtinfIdentyLoad(0);
                cProductsInfo.setDownedID1(0);
                iCProductsInfoService.updateById(cProductsInfo);
                SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
                String pdtInfPdt = cProductsInfo.getPdtinfPdt();
                sysLogBu.logRecord("修改品种", user.getTrueName(), "修改了产品名称为【" + pdtInfPdt + "】的产品的是否识别装载为【否】",plantId);
            }
        }
        if (planControllerStatus != null && !planControllerStatus.equals("")) {
            if (planControllerStatus.equals("1")) {
                cProductsInfo.setPdtinfPlanCtrl(1);
                cProductsInfo.setDownedID1(0);
                iCProductsInfoService.updateById(cProductsInfo);
                SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
                String pdtInfPdt = cProductsInfo.getPdtinfPdt();
                sysLogBu.logRecord("修改品种", user.getTrueName(), "修改了产品名称为【" + pdtInfPdt + "】的产品的是否开启计划控制为【是】",plantId);
            } else {
                cProductsInfo.setPdtinfPlanCtrl(0);
                cProductsInfo.setDownedID1(0);
                iCProductsInfoService.updateById(cProductsInfo);
                SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
                String pdtInfPdt = cProductsInfo.getPdtinfPdt();
                sysLogBu.logRecord("修改品种", user.getTrueName(), "修改了产品名称为【" + pdtInfPdt + "】的产品的是否开启计划控制为【否】",plantId);
            }
        }
        if (queueControllerStatus != null && !queueControllerStatus.equals("")) {
            if (queueControllerStatus.equals("1")) {
                cProductsInfo.setPdtinfQueue(1);
                cProductsInfo.setDownedID1(0);
                iCProductsInfoService.updateById(cProductsInfo);
                SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
                String pdtInfPdt = cProductsInfo.getPdtinfPdt();
                sysLogBu.logRecord("修改品种", user.getTrueName(), "修改了产品名称为【" + pdtInfPdt + "】的产品的是否开启队列控制为【是】",plantId);
            } else {
                cProductsInfo.setPdtinfQueue(0);
                cProductsInfo.setDownedID1(0);
                iCProductsInfoService.updateById(cProductsInfo);
                SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
                String pdtInfPdt = cProductsInfo.getPdtinfPdt();
                sysLogBu.logRecord("修改品种", user.getTrueName(), "修改了产品名称为【" + pdtInfPdt + "】的产品的是否开启队列控制为【否】",plantId);
            }
        }
        if (identifyQueueStatus != null && !identifyQueueStatus.equals("")){
            if (identifyQueueStatus.equals("1")){
                cProductsInfo.setPdtinfIdentyQueue(1);
                cProductsInfo.setDownedID1(0);
                iCProductsInfoService.updateById(cProductsInfo);
                SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
                String pdtInfPdt = cProductsInfo.getPdtinfPdt();
                sysLogBu.logRecord("修改品种", user.getTrueName(), "修改了产品名称为【" + pdtInfPdt + "】的产品的是否识别队列为【是】",plantId);
            }else {
                cProductsInfo.setPdtinfIdentyQueue(0);
                cProductsInfo.setDownedID1(0);
                iCProductsInfoService.updateById(cProductsInfo);
                SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
                String pdtInfPdt = cProductsInfo.getPdtinfPdt();
                sysLogBu.logRecord("修改品种", user.getTrueName(), "修改了产品名称为【" + pdtInfPdt + "】的产品的是否识别队列为【否】",plantId);
            }
        }

        if (inOutStatus != null && !inOutStatus.equals("")) {
            if (inOutStatus.equals("1")) {
                cProductsInfo.setPdtinfStockcls(1);
                cProductsInfo.setDownedID1(0);
                iCProductsInfoService.updateById(cProductsInfo);
                SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
                String pdtInfPdt = cProductsInfo.getPdtinfPdt();
                sysLogBu.logRecord("修改品种", user.getTrueName(), "将产品名称为【" + pdtInfPdt + "】的产品设置为进货类",plantId);
            } else {
                cProductsInfo.setPdtinfStockcls(0);
                cProductsInfo.setDownedID1(0);
                iCProductsInfoService.updateById(cProductsInfo);
                SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
                String pdtInfPdt = cProductsInfo.getPdtinfPdt();
                sysLogBu.logRecord("修改品种", user.getTrueName(), "将产品名称为【" + pdtInfPdt + "】的产品设置为出货类",plantId);
            }
        }
        return SzResult.Success();
    }
}
